/**
 * Copyright (c) 2009 Teoti Graphix, LLC See:
 *     http://tkarchitecture.teotigraphix.org
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * 
 * @author Michael Schmalle mschmalle@teotigraphix.com
 */
package org.teotigraphix.dx.patterns.manager.behavior
{

import flash.display.DisplayObject;
import flash.events.IEventDispatcher;

import org.teotigraphix.dx.core.ToolKitUtil;
import org.teotigraphix.dx.interfaces.IBehaviorLayer;
import org.teotigraphix.dx.interfaces.IBehaviorManager;
import org.teotigraphix.dx.interfaces.IComponentBehavior;
import org.teotigraphix.dx.patterns.manager.core.LayerRegistry;

[Mixin]

//[ExcludeClass]

/**
 * @private
 * 
 * The default <code>IBehaviorManager</code> implementation for the toolkit.
 * 
 * <p><em>This class is internal to the toolkit.</em></p>
 * 
 * @author Michael Schmalle
 * @copyright Teoti Graphix, LLC
 * @date 08-03-09
 */
public class BehaviorManagerImpl
	   implements IBehaviorManager
{
	include '../../../core/Version.as'
	
	//--------------------------------------------------------------------------
	//
	//  Public Static Initialization
	//
	//--------------------------------------------------------------------------
	
	ToolKitUtil.registerSingleton(
		"org.teotigraphix.dx.interfaces::IBehaviorManager", 
		BehaviorManagerImpl);
	
	//--------------------------------------------------------------------------
	//
	//  Public Mixin :: Methods - ISystemManager
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 */
	public static function init(root:DisplayObject):void
	{
	}
	
	//--------------------------------------------------------------------------
	//
	//  Private Class :: Properties
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 */
	private static var instance:IBehaviorManager = null;
	
	/**
	 * @private
	 */
	private static var registry:LayerRegistry = new BehaviorRegistry();
	
	//--------------------------------------------------------------------------
	//
	//  Constructor
	//
	//--------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 * @private
	 */
	public function BehaviorManagerImpl()
	{
	}
	
	//--------------------------------------------------------------------------
	//
	//  IToolKitBehaviorManager API :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @copy org.teotigraphix.dx.interfaces.IBehaviorManager#createBehaviorLayer()
	 */
	public function createBehaviorLayer(component:IEventDispatcher):IBehaviorLayer
	{
		return registry.createLayer(component) as IBehaviorLayer;
	}
	
	/**
	 * @copy org.teotigraphix.dx.interfaces.IBehaviorManager#getBehaviorLayer()
	 */
	public function getBehaviorLayer(component:IEventDispatcher):IBehaviorLayer
	{
		return registry.getLayer(component) as IBehaviorLayer;
	}
		
	/**
	 * @copy org.teotigraphix.dx.interfaces.IBehaviorManager#hasBehaviorLayer()
	 */
	public function hasBehaviorLayer(component:IEventDispatcher):Boolean
	{
		return registry.hasLayer(component);
	}
	
	// TODO creat unit test
		
	/**
	 * @copy org.teotigraphix.dx.interfaces.IBehaviorManager#registerBehaviorAPI()
	 */
	public function registerBehaviorAPI(
		api:Class, clazz:Class):void
	{
		registry.registerAPI(api, clazz);
	}
	
	// TODO creat unit test
	
	/**
	 * @copy org.teotigraphix.dx.interfaces.IBehaviorManager#createBehavior()
	 */
	public function createBehavior(
		component:IEventDispatcher, api:Class):IComponentBehavior
	{
		return registry.createClient(component, api) as IComponentBehavior;
	}
	
	/**
	 * @copy org.teotigraphix.dx.interfaces.IBehaviorManager#registerBehavior()
	 */
	public function registerBehavior(
		component:IEventDispatcher, api:Class, behavior:IComponentBehavior):void
	{
		registry.registerClient(component, api, behavior);
	}
		
	/**
	 * @copy org.teotigraphix.dx.interfaces.IBehaviorManager#removeBehavior()
	 */
	public function removeBehavior(
		component:IEventDispatcher, api:Class):IComponentBehavior
	{
		return registry.removeClient(component, api) as IComponentBehavior;
	}
		
	/**
	 * @copy org.teotigraphix.dx.interfaces.IBehaviorManager#hasBehavior()
	 */
	public function hasBehavior(
		component:IEventDispatcher, api:Class):Boolean
	{
		return registry.hasClient(component, api);
	}
		
	/**
	 * @copy org.teotigraphix.dx.interfaces.IBehaviorManager#hasBehavior()
	 */
	public function getBehavior(
		component:IEventDispatcher, api:Class):IComponentBehavior
	{
		return registry.getClient(component, api) as IComponentBehavior;
	}
		
	/**
	 * @copy org.teotigraphix.dx.interfaces.IBehaviorManager#getBehaviors()
	 */
	public function getBehaviors(component:IEventDispatcher):Array
	{
		return registry.getClients(component);
	}
	
	//--------------------------------------------------------------------------
	//
	//  Public Class :: Methods
	//
	//--------------------------------------------------------------------------
	
    /**
     * @private
     */
    public static function getInstance():IBehaviorManager
    {
        if (!instance) 
			instance = new BehaviorManagerImpl();
        return instance;
    }
}
}